package com.hongpu.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hongpu.config.SecurityContextUtil;
import com.hongpu.domain.CoolTowerInfo;
import com.hongpu.dto.CoolTowerInfoDto;
import com.hongpu.mapper.CoolTowerInfoMapper;
import com.hongpu.service.CoolTowerInfoService;
import com.hongpu.utils.ResultDto;
import com.hongpu.vo.SelectOptionVo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Michael zhang
 * @description 针对表【cool_tower_info(冷却塔信息)】的数据库操作Service实现
 * @createDate 2025-06-10 15:50:27
 */
@Service
public class CoolTowerInfoServiceImpl extends ServiceImpl<CoolTowerInfoMapper, CoolTowerInfo>
        implements CoolTowerInfoService {

    @Autowired
    CoolTowerInfoMapper baseMapper;

    /**
     * 新增
     *
     * @param coolTowerInfo
     */
    @Override
    public ResultDto add(CoolTowerInfo coolTowerInfo) {
        try {
            boolean save = save(coolTowerInfo);
            return save ? ResultDto.ok() : ResultDto.fail();
        } catch (Exception e) {
            return ResultDto.fail(e.getLocalizedMessage());
        }
    }

    /**
     * 修改
     *
     * @param coolTowerInfo
     */
    @Override
    public ResultDto update(CoolTowerInfo coolTowerInfo) {
        try {
            boolean update = updateById(coolTowerInfo);
            return update ? ResultDto.ok() : ResultDto.fail();
        } catch (Exception e) {
            return ResultDto.fail(e.getLocalizedMessage());
        }
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public ResultDto delete(Long id) {
        try {
            boolean delete = removeById(id);
            return delete ? ResultDto.ok() : ResultDto.fail();
        } catch (Exception e) {
            return ResultDto.fail(e.getLocalizedMessage());
        }
    }

    /**
     * 启用
     *
     * @param id
     */
    @Override
    public ResultDto activeById(Long id) {
        try {
            boolean active = baseMapper.activeById(id, SecurityContextUtil.getUserId());
            return active ? ResultDto.ok() : ResultDto.fail();
        } catch (Exception e) {
            return ResultDto.fail(e.getLocalizedMessage());
        }
    }

    /**
     * 分页
     *
     * @param coolTowerInfoDto
     */
    @Override
    public ResultDto page(CoolTowerInfoDto coolTowerInfoDto) {
        try {
            IPage<CoolTowerInfo> page = new Page<>(coolTowerInfoDto.getCurrent(), coolTowerInfoDto.getSize());
            IPage<CoolTowerInfo> selectPage = baseMapper.selectPage(page, coolTowerInfoDto);
            return ResultDto.ok(selectPage);
        } catch (Exception e) {
            return ResultDto.fail(e.getLocalizedMessage());
        }
    }

    /**
     * 下拉列表选项
     */
    @Override
    public ResultDto optionList() {
        try {
            List<SelectOptionVo<String>> result = new ArrayList<>();
            List<CoolTowerInfo> list = list();
            if (CollectionUtils.isNotEmpty(list)) {
                for (CoolTowerInfo coolTowerInfo : list) {
                    SelectOptionVo<String> selectOptionVo = new SelectOptionVo<>();
                    selectOptionVo.setLabel(coolTowerInfo.getModel());
                    selectOptionVo.setValue(coolTowerInfo.getModel());
                    result.add(selectOptionVo);
                }
            }
            return ResultDto.ok(result);
        } catch (Exception e) {
            return ResultDto.fail(e.getLocalizedMessage());
        }
    }

    /**
     * 根据冷却塔的型号新增
     *
     * @param model
     */
    @Override
    public boolean addByModel(String model) {
        boolean result = false;
        if(!isExistByModel(model)){
            try {
                CoolTowerInfo coolTowerInfo = new CoolTowerInfo();
                coolTowerInfo.setModel(model);
                result = save(coolTowerInfo);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return result;
    }

    /**
     * 判断某个冷却塔的型号是否存在
     *
     * @param model
     */
    @Override
    public boolean isExistByModel(String model) {
        boolean result = false;
        try  {
            result = baseMapper.isExistByModel(model);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    }
}




